package com.yll.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.yll.domain.Student;
import com.yll.domain.Users;
import com.yll.service.StudentService;
import com.yll.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
//@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @Reference
    private StudentService studentService;

    @RequestMapping(value = "/user/save",name = "用户注册")
    public String save(Users user, HttpServletRequest request) {
        boolean message = userService.save(user);
        //request.getSession().setAttribute("message", message);
        return "redirect:/index.jsp";

    }

    @RequestMapping(value = "/user/login",name = "用户登录")
    public String login(String username, String password, HttpServletRequest request) {
        Users user = userService.login(username, password);
        if (user != null) {
            return "forward:/student.jsp";
        } else {
            request.setAttribute("message","用户名或密码错误");
            return "forward:/index.jsp";
        }

    }

    @RequestMapping(value = "/student/findAll",name = "查询所有学生")
    public String findAll(@RequestParam(defaultValue = "1" ) Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, HttpServletRequest request) {
        PageInfo<Student> list = studentService.findAll(pageNum, pageSize);
        request.setAttribute("page", list);
        return "forward:/allStu.jsp";
    }

    @RequestMapping(value = "/student/findById",name = "查询单个学生")
    public String findById(Integer id,HttpServletRequest request) {
        Student student = studentService.findById(id);
        request.setAttribute("stu",student);
        return "forward:/itemDetail.jsp";
    }

    @RequestMapping(value = "/student/add",name = "添加学生")
    public String add(Student student) {
        boolean flag = studentService.save(student);
        return "forward:/student.jsp";
    }
}
